package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.mapper.OrderStatMapper;
import com.qianniu.napi.admin.service.*;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.AesUtil;
import com.qianniu.napi.common.util.DateUtil;
import com.qianniu.napi.common.util.EncryptUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 商家账务表
 * </p>
 *
 * @author chenxuebin
 * @since 2018-08-08
 */
@RestController
public class MaccountController {

    private static final Logger logger = LoggerFactory.getLogger(MaccountController.class);

    @Autowired
    private IMaccountService maccountService;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IJifenService jifenService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderStatService orderStatService;
    @Autowired
    private IChoujiangLogService choujiangLogService;
    @Autowired
    private IChoujiangService choujiangService;
    @Autowired
    private IOrderGoodsService orderGoodsService;
    @Autowired
    private ICouponTplService couponTplService;
    @Autowired
    private ICouponService couponService;
    @Autowired
    private IHexiaoLogService hexiaoLogService;

    @RequestMapping("/api/maccount/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "status", required = false) String status,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize,
                      HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        page = maccountService.selectPage(page, new EntityWrapper<Maccount>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/maccount/add")
    @ResponseBody
    public Jinfo maccountAdd(@Valid Maccount maccount,
                             BindingResult bindingResult, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            info.setMsg("There are errors! " + bindingResult);
            info.setCode(400);
            return info;
        }
        maccount.setCreatetime(new Date());
        maccount.insert();
        return info;
    }

    @RequestMapping("/api/maccount/modify")
    @ResponseBody
    public Jinfo maccountUpdate(HttpServletRequest req,
                                Maccount maccount) {
        Jinfo info = new Jinfo();
        maccount.setModifytime(new Date());
        maccount.updateById();
        return info;
    }

    @RequestMapping("/api/maccount/detail")
    @ResponseBody
    public Jinfo getMaccountDetailById(@RequestParam("token") String token,
                                       @RequestParam("id") long id, HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Maccount maccount = maccountService.selectById(id);
        info.setInfo(maccount);
        return info;
    }


    @RequestMapping("/api/mch/orderstat")
    @ResponseBody
    public Jinfo orderstat(@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        Wrapper<OrderStat> entity = new EntityWrapper<OrderStat>().orderBy("id",false);
        User ruser = redisService.getUser();
        if(ruser.getIsPlatform()==1){
            entity.eq("mpid",ObjectUtil.getMpid());
        }else if(ruser.getIsMch()==1){
            entity.eq("sid",ruser.getSid());
        }else{
            info.setCode(1);
            info.setMsg("无权限查看");
            return info;
        }

        page = orderStatService.selectPage(page,entity);
        List<OrderStat> list = page.getRecords();
        for(OrderStat item:list){

            List<Order> olist = ruser.getIsPlatform()==1?orderService.selectList(new EntityWrapper<Order>().setSqlSelect("order_no orderNo,realpay")
                    .where(" mpid=" + item.getMpid() + " and status>=2" + " and order_no like '" + item.getStatday().replaceAll("-", "") + "%'")):orderService.selectList(new EntityWrapper<Order>().setSqlSelect("order_no orderNo,realpay")
                    .where(" sid=" + item.getSid() + " and status>=2" + " and order_no like '" + item.getStatday().replaceAll("-", "") + "%'"));
            item.setOlist(olist);
        }

        info.setInfo(list);
        return info;
    }

}
